package cn.lzwei.bilimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.lzwei.bilimall.product.entity.BrandEntity;
import cn.lzwei.bilimall.product.service.BrandService;
import cn.lzwei.bilimall.product.vo.BrandVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.lzwei.bilimall.product.entity.CategoryBrandRelationEntity;
import cn.lzwei.bilimall.product.service.CategoryBrandRelationService;
import cn.lzwei.common.utils.PageUtils;
import cn.lzwei.common.utils.R;

import javax.annotation.Resource;


/**
 * 品牌分类关联
 *
 * @author lizewei
 * @email Lizewei2105@163.com
 * @date 2022-12-21 15:39:39
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 查询分类下的可供选择品牌，用于用户商品发布的基本信息填写
     */
    @RequestMapping("/brands/list")
    public R brandsList(@RequestParam(name = "catId") Long catId){
        List<CategoryBrandRelationEntity> brands= categoryBrandRelationService.getCategoryRelationBrands(catId);
        List<BrandVo> brandVos = brands.stream().map(item -> {
            BrandVo brandVo = new BrandVo();
            BeanUtils.copyProperties(item,brandVo);
            return brandVo;
        }).collect(Collectors.toList());
        return R.ok().put("data", brandVos);
    }

    /**
     * 获取品牌关联的所有分类列表
     */
    @GetMapping("/catelog/list")
    public R cateloglist(@RequestParam(name = "brandId") Long brandId){
        List<CategoryBrandRelationEntity> data=categoryBrandRelationService.list(
                new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId)
        );

        return R.ok().put("data", data);
    }

    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存品牌的分类信息：添加冗余字段 分类名+品牌名
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.saveDetail(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation){
		categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
